/*
 * ENUM Discoverer. ENUM Demonstration software, demonstrating the potential of ENUM.
 * 
 * Copyright (C) 2009 SIDN and ISOC.nl
 * 
 * This file is part of ENUM Discoverer.
 *
 * ENUM Discoverer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ENUM Discoverer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with ENUM Discoverer.  If not, see <http://www.gnu.org/licenses/>.
 */

package nl.sidn.enumdiscoverer.startup;

import java.util.Timer;
import java.util.TimerTask;

import nl.sidn.enumdiscoverer.EditPreferences;
import nl.sidn.enumdiscoverer.LogConfig;
import nl.sidn.enumdiscoverer.R;
import nl.sidn.enumdiscoverer.callhandling.CallBroadcastReceiver;
import nl.sidn.enumdiscoverer.sync.EnumSyncService;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;

/**
 * Sets up everything the application needs to run, then exits.
 * This includes ensuring default preferences are available (if none have been set by user),
 * starting {@link CallBroadcastReceiver}, and scheduling {@link EnumSyncService} runs.
 * If the application is already running, then this will start the preferences activity.
 * 
 * @author	Grant Patterson (grant.patterson@catalyst.net.nz)
 */
public class EnumLauncher extends Activity {

	static final String TAG = LogConfig.getLogTag(EnumLauncher.class);
	// whether DEBUG level logging is enabled (whether globally, or explicitly for this log tag)
	static final boolean DEBUG = LogConfig.isDebug(TAG);
	// whether VERBOSE level logging is enabled
	static final boolean VERBOSE = LogConfig.VERBOSE;
	
	private static final long CLOSE_DELAY = 3000;
	
	private final Timer mTimer = new Timer();
	
	/** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// needs to be called before we do anything else
		boolean isFirstRun = isFirstRun();
		
		StartupHelper.initEnvironment(this);
		
		StartupHelper.initIncallScreen(this);
		
		if (isFirstRun) {
			
			// ensure the sync service is scheduled if it is enabled
			// Note: we do this only on the first run so that the sync isn't rescheduled
			// every time the launcher is run (since we use the launcher to access preferences)
			StartupHelper.initSyncService(this);
			
			// display launch screen
			setContentView(R.layout.startup);
			finishAfterDelay();
			
		} else {
			// display preferences
			showPreferences();
			finish();
		}
	}
	
	@Override
	protected void onDestroy() {
		mTimer.cancel();
		super.onDestroy();
	}
	
	/**
	 * Try to determine whether this is the first time the application has been run
	 * @return true if this is the first time the application has been run
	 */
	private boolean isFirstRun() {
		
		String hasSetDefaultsKey = PreferenceManager.KEY_HAS_SET_DEFAULT_VALUES;
		
		// stores whether default values have been set yet
		SharedPreferences hasSetDefaults = getSharedPreferences(hasSetDefaultsKey, 0);
		
		// if the default values haven't been set, then assume it's the first launch
		return !hasSetDefaults.getBoolean(hasSetDefaultsKey, false);
	}
	
	/**
	 * Start the preferences activity
	 */
	private void showPreferences() {
		
		Intent intent = new Intent(this, EditPreferences.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);
	}
	
	/**
	 * Close this activity after the delay specified by CLOSE_DELAY
	 */
	private void finishAfterDelay() {
		
		// arrange to close after a short delay
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				finish();
			}
		};
		mTimer.schedule(task, CLOSE_DELAY);
	}
	
}
